<template>
    <div class="addPatent">
        <el-form :model="patentForm"  ref="ruleForm" label-width="120px">
            <el-form-item label="标题" prop="title">
                <el-col :span="20">
                    <el-input v-model="patentForm.title" placeholder="请输入标题"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="专利号" prop="number">
                <el-col :span="20">
                    <el-input v-model="patentForm.number" placeholder="请输入专利号"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="专利权人" prop="assignee">
                <el-col :span="20">
                    <el-input v-model="patentForm.assignee" placeholder="请输入专利权人"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="发明人" prop="inventor">
                <el-col :span="20">
                    <el-input v-model="patentForm.inventor" placeholder="请输入发明人"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="是否显示">
                <el-radio-group v-model="patentForm.flag">
                    <el-radio :label="'0'" selected>是</el-radio>
                    <el-radio :label="'1'">否</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addSubmit" :loading="loading">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import { addPatent,updatePatent } from '@/api/paperManage'
    export default {
        name: "AddPatent",
        props:{
            operateType: {
                type: String,
                default: () => 'add'
            },
            patentInfo: {
                type: Object,
                default: () => {
                    return {
                        patentid: '',
                        title: '',
                        number: '',
                        assignee: '',
                        inventor: '',
                        flag: '',
                    }
                }
            }
        },
        watch: {
            patentInfo: {
                handler(newVal, objVal) {
                    if(newVal !== objVal){
                        this.patentForm = newVal
                    }
                },
            }
        },
        data(){
            return{
                loading: false,
                patentForm: this.patentInfo
            }
        },
        methods:{
            async addSubmit() {
                if (this.operateType === 'add') {
                    try {
                        this.loading = true
                        const res = await addPatent(this.patentForm)
                        console.log(res)
                        this.$emit('updateTable')
                    } catch (e) {
                        console.log(new Error(e))
                    }
                    this.loading = false
                } else {
                    try {
                        this.patentForm.patentid = this.patentInfo.patentid
                        this.loading = true
                        const res = await updatePatent(this.patentForm)
                        this.$emit('updateTable')
                        console.log(res)
                    } catch (e) {
                        console.log(new Error(e))
                    }
                    this.loading = false
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .addPatent{
        /deep/ .el-form-item {
            margin-bottom: 15px;
        }
        /deep/ .el-form-item__label, .el-button--medium {
            font-size: 14px;
        }
        /deep/ .el-input__inner, .el-textarea__inner {
            font-size: 12px;
        }
    }

</style>
